//个人中心头部用户信息
'use strict';
import React,{Component} from 'react';
import Icon from 'react-native-vector-icons';
import AccountManager from './userManager';//账户管理
import NotifyInfo from './notifyInfo';//通知消息
import Util from '../common/util'
import * as URLconstant from '../constant/URLconstant'
import LinearGradient from 'react-native-linear-gradient'
import MyWallet from './userWallet/myWallet';//我的账户
import MyCoupon from './userCoupon/myCoupon';//我的优惠券
import MyIntegral from './userIntegral/myIntegral';//我的积分
import MyAchievement from './achievement/myAchievement';//我的业绩
import MyCommission from './commission/myCommission';//我的佣金
import ServiceFans from './userFans/serviceFans';//服务粉丝
import FansList from './userFans/fansList';//粉丝列表
import AppSetting from './appSetting';
import Toast from '../common/ToastUtil'
import DistributionOrder from './distribution/orderList'
import OrderInfo from '../order/orderInfo';
import {
    View,
    Text,
    Image,
    TouchableOpacity,
    Animated,
    StyleSheet, Platform, NativeModules, DeviceEventEmitter
} from 'react-native';
import QRCode from "../common/QRCode.ios";
const QRCodeAndroid =NativeModules.QRCode;

export default class userInfo extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            memberNowCoin:0
        };
        this.scanCodeBack=this.scanCodeBack.bind(this)
    }

    componentDidMount() {
       this.getCoin()
        DeviceEventEmitter.addListener("scanCode", this.scanCodeBack)
    }


    linkJump(name, user_id) {
        let that = this;
        that.props.navigator.push({
            component: name,
            passProps: {
                user_id: user_id,
            }
        })
    }

    getCoin(){
        let url=URLconstant.MY_COIN
        let datakey=[]
        datakey['user_id']=this.props.datas.user_id
        Util.get(url,datakey,this.getCoinSuc.bind(this),this.errorCallback.bind(this))
    }

    getCoinSuc(datas){
        if(datas.status==0){
            this.setState({
                memberNowCoin:datas.data.memberNowCoin
            })
        }
    }
    errorCallback(err){

    }

    changeCenter(){
        let showCenter = this.props.showCenter
        showCenter=showCenter==0?1:0
        this.props.changeCenter(showCenter)
    }


    //打开扫描二维码
    onClickQRcode() {
        let that = this;
        if (Platform.OS === 'android') {
            var markName = "scanCode";
            QRCodeAndroid.initQRCode(markName);
        } else {
            that.props.navigator.push(
                {
                    component: QRCode,
                    passProps: {
                        saveQRCode: that.scanCodeBack
                    }
                }
            );
        }
    }


    scanCodeBack(datas){
        LOG('code_type==='+datas)
        let code_type = datas.split('_')[1]
        let value = datas.split('|')[1]
        code_type=parseInt(code_type)
        LOG('code_type==='+code_type)
        if(code_type==12){
            let url = URLconstant.GET_CODE_ORDER
            let datakey = [];
            datakey['user_id'] = this.props.datas.user_id;
            datakey['order_sn'] = value
            Util.get(url,datakey,this.getCodeOrder.bind(this),this.errorCallback.bind(this))
        }else if(code_type==9){
            let url = URLconstant.USE_VOUCHE
            let datakey = [];
            datakey['vouche_sn'] = value
            datakey['user_id'] = this.props.datas.user_id;
            datakey['finish_server'] = 1
            Util.get(url, datakey, this.useSuccess.bind(this), this.errorCallback.bind(this))
        }else if(code_type==10){
            if(Util.isIos()){
                this.props.navigator.replace({
                    component:OrderInfo,
                    passProps:{
                        order_sn:value,
                        isExtract:true,
                        user_id:this.props.datas.user_id
                    }
                })
            }else{
                this.props.navigator.push({
                    component:OrderInfo,
                    passProps:{
                        order_sn:value,
                        isExtract:true,
                        user_id:this.props.datas.user_id
                    }
                })
            }

        }
    }

    //使用服务券码
    useSuccess(datas){
        LOG('datas====='+JSON.stringify(datas))
        Toast.show(datas.msg)
        if(Util.isIos()){
            this.props.navigator.pop()
        }
    }

    //获取二维码订单信息
    getCodeOrder(datas){
        LOG('code_type===='+datas.data.order_sn)
        if(datas.status==0){
            let order_sn = datas.data.order_sn
            let delivery_status = datas.data.delivery_status
            if(Util.isIos()){
                this.props.navigator.replace({
                    component:DistributionOrder,
                    passProps:{
                        order_sn:order_sn,
                        delivery_status:delivery_status,
                        user_id:this.props.datas.user_id,
                        order_type:0,
                        role_id:this.props.role_id,
                        shop_id:this.props.shop_id,
                        identity_id:this.props.identity_id
                    }
                })
            }else{
                this.props.navigator.push({
                    component:DistributionOrder,
                    passProps:{
                        order_sn:order_sn,
                        delivery_status:delivery_status,
                        user_id:this.props.datas.user_id,
                        order_type:0,
                        role_id:this.props.role_id,
                        shop_id:this.props.shop_id,
                        identity_id:this.props.identity_id
                    }
                })
            }

        }else{
            Toast.show(dats.msg)
        }
    }


    showUserInfo(role_name){
        let datas = this.props.datas;
        let data = datas.data;
        let user_id = datas.user_id;
        let user_info = data.userInfo;
        let showCenter = this.props.showCenter
        let role_id = this.props.role_id
        let centerTxt=''
        let identity_id = this.props.identity_id
        if(role_id==8&&identity_id==8){
            centerTxt='配送员中心>'
        }else if(role_id!=8&&identity_id==8){
            centerTxt='店员店长/配送员中心>'
        }else {
            centerTxt='店长店员中心>'
        }
        return(
            <View style={{height:200,justifyContent:'space-between'}}>
                <View style={styles.header_setting_view}>
                    <View style={styles.user_name_portrait}>
                        {
                            user_info.portrait==''||user_info.portrait=='n'?
                                <Image
                                    source={require('../imgs/user.png')}
                                    resizeMode={'cover'}
                                    style={{width:50,height:50,borderRadius:25}}
                                />
                                :
                                <Image
                                    source={{uri:user_info.portrait}}
                                    resizeMode={'cover'}
                                    style={{width:50,height:50,borderRadius:25}}
                                />
                        }
                        <View style={styles.user_name_wrap}>
                            <Text style={{fontSize:14,color:"#fff",marginBottom:5,backgroundColor:'transparent'}}>{user_info.user_name==''?user_info.real_name:user_info.user_name}({user_id})</Text>
                            <Text style={{fontSize:12,color:"#fff",backgroundColor:'transparent'}}>{role_name}</Text>
                        </View>
                    </View>
                    <View style={styles.center_btn_view}>
                        {
                            role_id==0?null: <TouchableOpacity style={styles.center_btn} onPress={()=>this.onClickQRcode()}>
                                <Icon style={{backgroundColor:'transparent'}} name={'md-qr-scanner'} size={25} color={'#fff'}/>
                            </TouchableOpacity>
                        }
                        <TouchableOpacity style={styles.center_btn} onPress={()=>this.linkJump(AppSetting)}>
                            <Image
                                source={require('../imgs/setting_app.png')}
                                style={{width:25,height:25}}
                                resizeMode={'cover'}
                            />
                        </TouchableOpacity>
                    </View>
                </View>
                {
                    role_id==0?
                        null:
                        <TouchableOpacity onPress={()=>this.changeCenter()} style={styles.chanCenterBtn}>
                            <Text style={{fontSize:14,color:"#fff",backgroundColor:'transparent'}}>{showCenter==0?centerTxt:'会员中心>'}</Text>
                        </TouchableOpacity>
                }
                {
                    showCenter==0?
                        <View style={styles.user_three_wrap}>
                            <TouchableOpacity onPress={()=>this.linkJump(MyWallet,user_id)} style={styles.user_three_item}>
                                <Text style={{fontSize:16,color:"#fff",backgroundColor:'transparent'}}>{user_info.surplus}</Text>
                                <Text style={{fontSize:12,color:"#fff",backgroundColor:'transparent'}}>账户</Text>
                            </TouchableOpacity>
                            <TouchableOpacity onPress={()=>this.linkJump(MyCoupon,user_id)} style={styles.user_three_item}>
                                <Text style={{fontSize:16,color:"#fff",backgroundColor:'transparent'}}>{user_info.coupon_nums}</Text>
                                <Text style={{fontSize:12,color:"#fff",backgroundColor:'transparent'}}>优惠券</Text>
                            </TouchableOpacity>
                            <TouchableOpacity onPress={()=>this.linkJump(MyIntegral,user_id)} style={styles.user_three_item}>
                                <Text style={{fontSize:16,color:"#fff",backgroundColor:'transparent'}}>{this.state.memberNowCoin}</Text>
                                <Text style={{fontSize:12,color:"#fff",backgroundColor:'transparent'}}>积分</Text>
                            </TouchableOpacity>
                        </View>
                        :
                        <View>
                            {
                                role_id!=8?
                                    <View style={styles.user_three_wrap}>
                                        <TouchableOpacity onPress={()=>this.linkJump(MyAchievement,user_id)} style={styles.user_three_item}>
                                            <Text style={{fontSize:16,color:"#fff",backgroundColor:'transparent'}}>{data.performanceInfo.allPerformance}</Text>
                                            <Text style={{fontSize:12,color:"#fff",backgroundColor:'transparent'}}>我的业绩</Text>
                                        </TouchableOpacity>
                                        <TouchableOpacity onPress={()=>this.linkJump(MyCommission,user_id)} style={styles.user_three_item}>
                                            <Text style={{fontSize:16,color:"#fff",backgroundColor:'transparent'}}>{data.agent_info.income}</Text>
                                            <Text style={{fontSize:12,color:"#fff",backgroundColor:'transparent'}}>我的佣金</Text>
                                        </TouchableOpacity>
                                        <TouchableOpacity onPress={()=>this.linkJump(ServiceFans,user_id)} style={styles.user_three_item}>
                                            <Text style={{fontSize:16,color:"#fff",backgroundColor:'transparent'}}>{data.performanceInfo.allFansCount}</Text>
                                            <Text style={{fontSize:12,color:"#fff",backgroundColor:'transparent'}}>服务粉丝</Text>
                                        </TouchableOpacity>
                                        <TouchableOpacity onPress={()=>this.linkJump(FansList,user_id)} style={styles.user_three_item}>
                                            <Text style={{fontSize:16,color:"#fff",backgroundColor:'transparent'}}>{data.agent_info.fans_count}</Text>
                                            <Text style={{fontSize:12,color:"#fff",backgroundColor:'transparent'}}>我的粉丝</Text>
                                        </TouchableOpacity>
                                    </View>:
                                    null
                            }

                        </View>
                }

            </View>
        )
    }

    render() {

        let startColor= '#5886ff'
        let endColor= '#329cfa'
        let role_name = ''
        let role_id = this.props.role_id
        let identity_id = this.props.identity_id
        if(role_id==0){
            role_name='(消费者)'
        }else if(role_id==1){
            role_name='(店长)'
        }else if(role_id==3&&identity_id==0){
            role_name='(店员)'
        }else if(role_id==8){
            role_name='(配送员)'
        }else if(role_id==3&&identity_id==8){
            role_name='(店员)(配送员)'
        }
        return (
            <View style={{padding:10}}>
                <LinearGradient colors={[startColor, endColor]}
                                start={{x: 0, y: 0}}
                                end={{x: 1, y: 0}}
                                style={{borderRadius:10}}
                                locations={[0, 0.75]}>
                    {this.showUserInfo(role_name)}
                </LinearGradient>
            </View>
        )
    }
}
var styles = StyleSheet.create({
    header_setting_view:{
        flexDirection:"row",
        alignItems:'center',
        justifyContent:'space-between',
        paddingHorizontal:10,
        paddingVertical:15,
    },
    user_name_portrait:{
        flexDirection:"row",
        alignItems:'center',
        justifyContent:'center',
        flex:1,
    },
    portrait_wrap:{
        alignItems:'center',
        justifyContent:'center',
        width:54,
        height:54,
        borderRadius:27,
        backgroundColor:"#fff"
    },
    user_name_wrap:{
        flex:1,
        paddingLeft:10,
    },
    user_three_wrap:{
        flexDirection:'row',
        alignItems:'center',
    },
    user_three_item:{
        flex:1,
        alignItems:'center',
        justifyContent:'center',
        paddingBottom:30
    },
    chanCenterBtn:{
        height:25,
        alignItems:'center',
        justifyContent:'center',
        paddingHorizontal:10,
        backgroundColor:'rgba(0,0,0,.5)',
        position:'absolute',
        top:85,
        right:0,
        borderTopLeftRadius:12.5,
        borderBottomLeftRadius:12.5,
        borderTopRightRadius:Util.isIos()?12.5:0,
        borderBottomRightRadius:0
    },
    center_btn_view:{
        flexDirection:'row',
        alignItems:'center',
        justifyContent:'center'
    },
    center_btn:{
        width:25,
        height:25,
        alignItems:'center',
        justifyContent:'center',
        marginLeft:10
    }
})